import { request } from '../http'

// 领域管理相关API
export const domainApi = {
  // 获取启用的领域列表 - 暂时注释，如需要可手动启用
  // getEnabledDomains: () => request.Get('/domains/enabled'),

  // 获取领域详情
  getDomainDetail: (id: number) => request.Get(`/domains/${id}`),

  // 管理员获取领域列表
  getDomainList: (params: any) => request.Get('/domains/admin/list', { params }),

  // 创建领域
  createDomain: (data: any) => request.Post('/domains/admin/create', data),

  // 更新领域
  updateDomain: (id: number, data: any) => request.Put(`/domains/admin/${id}`, data),

  // 删除领域
  deleteDomain: (id: number, password: string) => request.Delete(`/domains/admin/${id}`, { password })
}

// 服务联动配置相关API
export const serviceIntegrationApi = {
  // 获取服务配置列表
  getServiceList: () => request.Get('/service-integrations/admin/list'),

  // 获取指定服务配置
  getServiceConfig: (serviceName: string) => request.Get(`/service-integrations/admin/${serviceName}`),

  // 更新服务配置
  updateServiceConfig: (serviceName: string, data: any) => request.Put(`/service-integrations/admin/${serviceName}`, data),

  // 测试服务连接
  testServiceConnection: (serviceName: string, data: any) => request.Post(`/service-integrations/admin/${serviceName}/test`, data),

  // 调用外部API（通用方法）
  callExternalApi: (serviceName: string, apiName: string, params: any) => request.Post(`/service-integrations/call/${serviceName}/${apiName}`, { params }),

  // 子系统K专用API调用（推荐使用，更好的错误处理和类型安全）
  callSystemKApi: (apiName: string, params: any = {}) => request.Post(`/service-integrations/system-k/${apiName}`, { params }),

  // 初始化默认配置
  initDefaultConfig: () => request.Post('/service-integrations/admin/init'),

  // 清除图片URL模板缓存
  clearImageCache: () => request.Post('/service-integrations/admin/clear-image-cache')
}

// 专家管理相关API
export const expertApi = {
  // 获取启用的专家列表 - 暂时注释，如需要可手动启用
  // getEnabledExperts: (params?: any) => request.Get('/experts/enabled', { params }),

  // 获取专家详情
  getExpertDetail: (id: number) => request.Get(`/experts/${id}`),

  // 管理员获取专家列表
  getExpertList: (params: any) => request.Get('/experts/admin/list', { params }),

  // 创建专家
  createExpert: (data: any) => request.Post('/experts/admin/create', data),

  // 更新专家
  updateExpert: (id: number, data: any) => request.Put(`/experts/admin/${id}`, data),

  // 删除专家
  deleteExpert: (id: number, password: string) => request.Delete(`/experts/admin/${id}`, { password }),

  // 上传专家头像
  uploadAvatar: (id: number, file: FormData) => request.Post(`/experts/admin/${id}/avatar`, file),

  // 上传专家展示图片
  uploadDisplayImages: (id: number, files: FormData) => request.Post(`/experts/admin/${id}/display-images`, files)
}

// 用户管理相关API
export const userApi = {
  // 管理员获取用户列表
  getUserList: (params: any) => request.Get('/user/admin/list', { params }),

  // 获取用户详情
  getUserDetail: (id: number) => request.Get(`/user/admin/${id}`),

  // 获取用户订单列表
  getUserOrders: (id: number, params: any) => request.Get(`/user/admin/${id}/orders`, { params }),

  // 获取用户问答记录
  getUserQuestions: (id: number, params: any) => request.Get(`/user/admin/${id}/questions`, { params }),

  // 获取用户日志
  getUserLogs: (id: number, params: any) => request.Get(`/user/admin/${id}/logs`, { params }),

  // 获取用户统计数据
  getUserStats: () => request.Get('/user/admin/stats'),

  // 管理员给用户兑换兑换码
  redeemCodeForUser: (userId: number, data: any) => request.Post(`/user/admin/${userId}/redeem`, data),

  // 获取可用兑换码列表
  getAvailableRedeemCodes: () => request.Get('/user/admin/available-redeem-codes')
}

// 问答管理相关API
export const questionApi = {
  // 用户提交问题
  askQuestion: (data: any) => request.Post('/questions/ask', data),

  // 用户对答案评分
  rateAnswer: (id: number, data: any) => request.Post(`/questions/${id}/rating`, data),

  // 获取用户问答历史
  getMyQuestions: (params: any) => request.Get('/questions/my-questions', { params }),

  // 获取问题详情
  getQuestionDetail: (id: number) => request.Get(`/questions/${id}`),

  // 管理员获取问答列表
  getQuestionList: (params: any) => request.Get('/questions/admin/list', { params }),

  // 获取问答统计数据
  getQuestionStats: () => request.Get('/questions/admin/stats')
}

// 订单管理相关API
export const orderApi = {
  // 创建订单
  createOrder: (data: any) => request.Post('/orders/create', data),
  
  // 获取用户订单列表
  getMyOrders: (params: any) => request.Get('/orders/my-orders', { params }),
  
  // 获取订单详情
  getOrderDetail: (orderNo: string) => request.Get(`/orders/${orderNo}`),
  
  // 取消订单
  cancelOrder: (orderNo: string) => request.Post(`/orders/${orderNo}/cancel`),
  
  // 支付回调
  paymentCallback: (data: any) => request.Post('/orders/payment/callback', data),
  
  // 管理员获取订单列表
  getOrderList: (params: any) => request.Get('/orders/admin/list', { params }),
  
  // 管理员获取订单详情
  getAdminOrderDetail: (orderNo: string) => request.Get(`/orders/admin/${orderNo}`),

  // 管理员获取订单统计
  getOrderStats: () => request.Get('/orders/admin/stats')
}

// 兑换码管理相关API
export const redeemCodeApi = {
  // 用户兑换码兑换
  redeemCode: (data: any) => request.Post('/redeem-codes/redeem', data),
  
  // 管理员获取兑换码列表
  getRedeemCodeList: (params: any) => request.Get('/redeem-codes/admin/list', { params }),
  
  // 创建兑换码
  createRedeemCode: (data: any) => request.Post('/redeem-codes/admin/create', data),
  
  // 批量创建兑换码
  batchCreateRedeemCode: (data: any) => request.Post('/redeem-codes/admin/batch-create', data),
  
  // 获取兑换码详情
  getRedeemCodeDetail: (id: number) => request.Get(`/redeem-codes/admin/${id}`),
  
  // 删除兑换码
  deleteRedeemCode: (id: number) => request.Delete(`/redeem-codes/admin/${id}`),
  
  // 获取兑换码统计数据
  getRedeemCodeStats: () => request.Get('/redeem-codes/admin/stats'),

  // 获取批次列表
  getBatchList: () => request.Get('/redeem-codes/admin/batches')
}

// 任务管理相关API
export const taskApi = {
  // 获取任务列表
  getTaskList: (params: any) => request.Get('/tasks/list', { params }),

  // 获取重要任务（用于通知）
  getImportantTasks: () => request.Get('/tasks/important'),

  // 获取任务详情
  getTaskDetail: (id: number) => request.Get(`/tasks/${id}`),

  // 创建任务
  createTask: (data: any) => request.Post('/tasks', data),

  // 更新任务
  updateTask: (id: number, data: any) => request.Put(`/tasks/${id}`, data),

  // 删除任务
  deleteTask: (id: number) => request.Delete(`/tasks/${id}`),

  // 批量操作任务
  batchUpdateTasks: (data: any) => request.Post('/tasks/batch', data)
}

// 认证相关API
export const authApi = {
  // 发送验证码
  sendCode: (data: any) => request.Post('/auth/send-code', data),

  // 用户登录/注册
  login: (data: any) => request.Post('/auth/login', data),

  // 获取用户信息
  getUserInfo: () => request.Get('/auth/me'),

  // 更新用户信息
  updateProfile: (data: any) => request.Put('/auth/profile', data),

  // 修改密码
  changePassword: (data: any) => request.Put('/auth/password', data),

  // 刷新令牌
  refreshToken: () => request.Post('/auth/refresh'),

  // 退出登录
  logout: () => request.Post('/auth/logout')
}

// 管理员认证相关API
export const adminApi = {
  // 管理员登录
  login: (data: any) => request.Post('/admin/login', data),

  // 获取管理员信息
  getAdminInfo: () => request.Get('/admin/me'),

  // 更新管理员个人信息
  updateProfile: (data: any) => request.Put('/admin/profile', data),

  // 修改管理员密码
  changePassword: (data: any) => request.Put('/admin/password', data),

  // 获取管理员列表
  getAdminList: (params: any) => request.Get('/admin/list', { params }),

  // 创建管理员
  createAdmin: (data: any) => request.Post('/admin/create', data),

  // 编辑管理员信息
  editAdmin: (id: number, data: any) => request.Put(`/admin/edit/${id}`, data),

  // 删除管理员
  deleteAdmin: (id: number) => request.Delete(`/admin/${id}`),

  // 重置管理员密码
  resetPassword: (data: any) => request.Post('/admin/reset-password', data),

  // 更新管理员状态
  updateStatus: (data: any) => request.Put('/admin/status', data),

  // 退出登录
  logout: () => request.Post('/admin/logout')
}

// 导出所有API
export default {
  domain: domainApi,
  expert: expertApi,
  user: userApi,
  question: questionApi,
  order: orderApi,
  redeemCode: redeemCodeApi,
  task: taskApi,
  auth: authApi,
  admin: adminApi
}
